# StartTCL: n
########################################
#      zJail SyStem v1.4.9 by Rama      #
########################################
#
# Comments:
# This is a new Jail SyStem,
# it is more handly and effectiver
#
# Credits:
# To Spirit, for much Help^^ :)) and
# To Spirit, for the sqlcustom commands^^
# To Lazarus for the Converter^^
#
# you can add in your system or procs where you need this lines:
#
# ::zJail::Check player  ;//Checks if player must be jailed or freed
# ::zJail::JailPlayer player <mins> <reason> ;// Jail the Player ;; <...> = <If you want> if you dont write anything = unlimited ;; reason same.. if you don't write, nothing you will see...
# ::zJail::JailOfflinePlayer CHARNAME <mins> <reason> :// Jail a offline player, but with charname...
# ::zJail::FreePlayer player ;// Free a Jailed player
# More Comming if Requested
#


namespace eval ::zJail {
	#
	variable VERSION 1.4.9
	#
	#
	# You can set here the Jail coordinates, where the Jail is
	variable JailPosition "13 0 0 0"
	#
	#
	# If you enable it, than it's impossible to comes out, if you're normal jailed
	# But this is not Good for Ping, it increases a little bit
	variable CheckOnAgro 0
	#
	#
	# If you have a small server, or Jailed player must be kicked, till they freed, enable it
	variable KickIfJailed 0
	#
	#
	# If Enabled and you are Jailed, you cant use Teleport commands...
	variable NoCmdTeleIfJailed 1
	#
	#
	# If you enable this option, the player gets Jailed if he kill Civilian...
	# if player kill 10, jailed for 1min, if 20, jailed for 2min, if 30 jailed for 3min...
	variable AutoJailIfKillCivilians 0
	#
	#
	# variable JAIL_DIR "extra/jail"
	variable JAIL_DIR "jail"
	#
	#
	#
	#
	if { ! [ file exists $JAIL_DIR ] } {file mkdir $JAIL_DIR}
	set min_custom_version 1.98
	set customversion [::Custom::GetScriptVersion ::Custom]
	if { $customversion < $min_custom_version } { puts "[ ::Custom::LogPrefix ]You need Custom.tcl at least v$min_custom_version!" }
}

proc ::zJail::jail { player cargs } {
	set cargs [ split $cargs ]
	switch -- [ lindex $cargs 0 ] {
		"list" {
			set out {}
			foreach jailfile [glob -types f -nocomplain -tails -directory $::zJail::JAIL_DIR "*"] {
				if { [ lindex [ split $jailfile "_" ] 1 ] == "free" } { continue }
				set xfile [open "$::zJail::JAIL_DIR/$jailfile"]
				set data [read $xfile]
				close $xfile
				if { [ lindex $data 0 ] != 0 } { set jailedtill "[::Texts::Get jailed_till] [clock format [ lindex $data 0 ] -format "%d.%m (%Hh:%Mm)"]. (Rest: [::Custom::SecondsToTime [ expr { [ lindex $data 0 ] - [clock seconds] } ]])"
				} else { set jailedtill "" }
				if { [ lindex $data 1 ] != 0 } { set reason "[::Texts::Get reason_why] [lrange $data 1 end]"
				} else { set reason "" }
				lappend out "$jailfile $jailedtill $reason"
			}
			if { [llength $out] } { return [ join $out \n ] }
			return [::Texts::Get no_one_jailed]
		}
		"free" {
			if { [ lindex $cargs 1 ] == "" } {
			set SelPlay [ ::GetSelection $player ]
			if { $SelPlay == 0 } { return [::Texts::Get no_selected] }
			set fname [ ::GetName $SelPlay ]
			if { [ file exists "$::zJail::JAIL_DIR/$fname" ] } {
			::zJail::FreePlayer $SelPlay
			return [::Texts::Get player_freed $fname]
			} else { return "$fname is not Jailed" }
			} else {
			set freename [ lindex $cargs 1 ]
			if { [ file exists "$::zJail::JAIL_DIR/$freename" ] } {
			set playeridname [ ::Custom::GetPlayerID $freename ]
			if { ![ ::Custom::IsOnline $playeridname ] } {
			file delete "$::zJail::JAIL_DIR/$freename"
			set filefree [ open "$::zJail::JAIL_DIR/${freename}_free" w+ ]
			puts $filefree "1"
			close $filefree
			return [::Texts::Get offline_player $freename]
			} else {
			::zJail::FreePlayer $playeridname
			return [::Texts::Get player_freed $freename] }
			} else { return [::Texts::Get not_jailed $freename] } }
		}
		"help" { return [::Texts::Get jail_help] }
		default {
		set selection [::GetSelection $player]
		if { $selection != 0 } {
			set object_type [::GetObjectType $selection]
			if { $object_type == 4 } {
				set playerid [ ::GetSelection $player ]
				if { [ ::GetPlevel $playerid ] >= [ ::GetPlevel $player ] } { return [::Texts::Get cant_jail_plevel] }
				if { [ lindex $cargs 0 ] == "" } { ::zJail::JailPlayer $playerid
				set jailname [::GetName $playerid]
				return [::Texts::Get jail_till_gmfree $jailname]
				} else {
				if { [string is integer -strict [ lindex $cargs 0 ]] } { set jailtime [ lindex $cargs 0 ]
				} else {
				set reason [lrange $cargs 0 end]
				set jailtime 0 }
				if { [ lindex $cargs 1 ] != "" && $jailtime != 0 } { set reason [lrange $cargs 1 end]
				} else { set reason 0 }
				::zJail::JailPlayer $playerid $jailtime $reason
				if { $jailtime == 0 } { set jailtext [::Texts::Get jail_till_gmfree2]
				} else { set jailtext "[clock format [ expr { [clock seconds] + ($jailtime * 60) } ] -format "%d.%m (%Hh:%Mm)"]" }
				return "[ ::GetName $playerid ] [::Texts::Get is_jailed] $jailtext" }
				} else { return [::Texts::Get no_selected] }
				} else {
				if { [ lindex $cargs 0 ] == "" } { return [::Texts::Get jail_help] }
				set playerid [ ::Custom::GetPlayerID [ lindex $cargs 0 ] ]
				if { [ ::Custom::IsOnline $playerid ] } {
				if { [ ::GetPlevel $playerid ] >= [ ::GetPlevel $player ] } { return [::Texts::Get cant_jail_plevel] }
				if { [ lindex $cargs 1 ] == "" } { ::zJail::JailPlayer $playerid
				set jailname [::GetName $playerid]
				return [::Texts::Get jail_till_gmfree $jailname]
				} elseif { ![string is integer -strict [ lindex $cargs 1 ]] } {
				set jailtime 0
				set reason [lrange $cargs 1 end]
				} else {
				set jailtime [ lindex $cargs 1 ]
				if { [ lindex $cargs 2 ] == "" } { set reason 0
				} else { set reason [lrange $cargs 2 end] } }
				::zJail::JailPlayer $playerid $jailtime $reason
				if { $jailtime == 0 } { set jailtext [::Texts::Get jail_till_gmfree2]
				} else { set jailtext "[clock format [ expr { [clock seconds] + ($jailtime * 60) } ] -format "%d.%m (%Hh:%Mm)"]" }
				return "[ ::GetName $playerid ] [::Texts::Get is_jailed] $jailtext"
				} else {
				if { [ lindex $cargs 1 ] == "" } {
				set tojailname [ lindex $cargs 0 ]
				::zJail::JailOfflinePlayer $jailname
				return [::Texts::Get if_online_jail $tojailname]
				} else {
				if { ![string is integer -strict [ lindex $cargs 1 ]] } {
				set jailtime 0
				set reason [lrange $cargs 1 end]
				} else {
				set jailtime [ lindex $cargs 1 ]
				if { [ lindex $cargs 2 ] == "" } { set reason 0
				} else { set reason [lrange $cargs 2 end] } } }
				::zJail::JailOfflinePlayer [ lindex $cargs 0 ] $jailtime $reason
				if { $jailtime == 0 } { set jailtext "till a GM Free him!"
				} else { set jailtext "till [clock format [ expr { [clock seconds] + ($jailtime * 60) } ] -format "%d.%m (%Hh:%Mm)"]" }
				set tojailname [ lindex $cargs 0 ]
				return "[::Texts::Get if_online_jail $tojailname] $jailtext" }
			}
		}
	}
}

proc ::zJail::JailPlayer { player {freedtime 0} {reason 0} } {
	set fcargs [ ::GetName $player ]
	if { $freedtime != 0 } { set wholetime [ expr { [clock seconds] + ($freedtime * 60) } ]
} else { set wholetime 0 }
	set file [open "$::zJail::JAIL_DIR/$fcargs" w+]
	puts $file "$wholetime $reason"
	close $file
	::zJail::JailPos $player
	if { $::zJail::KickIfJailed } { KickPlayer $player }
}

proc ::zJail::JailOfflinePlayer { player {freedtime 0} {reason 0} } {
	if { $freedtime != 0 } { set wholetime [ expr { [clock seconds] + ($freedtime * 60) } ]
} else { set wholetime 0 }
	set file [open "$::zJail::JAIL_DIR/$player" w+]
	puts $file "$wholetime $reason"
	close $file
}

proc ::zJail::JailPos { player } {
	::Say $player 0 "[::Texts::Get now_jailed][ ::Custom::Sound levelup ]"
	::Custom::TeleportPos $player $::zJail::JailPosition
}

proc ::zJail::Check { to {from 0} {spellid 0} } {
	set pname [ ::GetName $to ]
	if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } { ::zJail::JailPos $to }
	if { [ file exists "$::zJail::JAIL_DIR/${pname}_free" ] } {
		file delete "$::zJail::JAIL_DIR/${pname}_free"
		::Custom::GoHome $to
	}
}

proc ::zJail::CheckStartSpell { to {from 0} {spellid 0} } {
set pname [ ::GetName $to ]
if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } {
set xfile [open "$::zJail::JAIL_DIR/$pname"]
set data [read $xfile]
close $xfile
if { [ lindex $data 0 ] != 0 } { set jailedtime [ lindex $data 0 ]
} else {
if { $::zJail::KickIfJailed } { KickPlayer $to }
::zJail::JailPos $to; return }
if { [clock seconds] > $jailedtime } {
file delete "$::zJail::JAIL_DIR/$pname"
::Custom::GoHome $to; return
} else {
if { $::zJail::KickIfJailed } { KickPlayer $to }
::zJail::JailPos $to; return } }
if { [ file exists "$::zJail::JAIL_DIR/${pname}_free" ] } {
file delete "$::zJail::JAIL_DIR/${pname}_free"
::Custom::GoHome $to }
}

proc ::zJail::FreePlayer { player } {
	set pname [ ::GetName $player ]
	if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } {
	file delete "$::zJail::JAIL_DIR/$pname"
	::Custom::GoHome $player
	}
}

proc ::zJail::free { player cargs } {
set pname [ ::GetName $player ]
	if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } {
		set xfile [open "$::zJail::JAIL_DIR/$pname"]
		set data [read $xfile]
		close $xfile
		if { [ lindex $data 0 ] != 0 } { set jailedtime [ lindex $data 0 ]
		} else { return [::Texts::Get player_till_gmfree] }
		if { [clock seconds] > $jailedtime } { ::Custom::GoHome $player
		file delete "$::zJail::JAIL_DIR/$pname"
		return [::Texts::Get back_home]
		} else {
		set waittime [::Custom::SecondsToTime [ expr { [ lindex $data 0 ] - [clock seconds] } ]]
		return [::Texts::Get player_wait_tillfree $waittime] }
	} else { return [::Texts::Get player_not_jailed] }
}

::Custom::AddCommand "free" "::zJail::free"
::Custom::AddCommand "jail" "::zJail::jail" 4
::Custom::AddSpellScript "::zJail::CheckStartSpell" 836
if { $::zJail::CheckOnAgro } {::Custom::HookProc "::AI::CanAgro" {::zJail::Check $player}}
if { $::zJail::AutoJailIfKillCivilians } {
proc ::zJail::AutoJailbyKillCivilian { player npc } {
if { [ ::GetCreatureType $npc ] == 8 } {
set KillCivilianCount #############################################################
set nToJail [ expr { $KillCivilianCount / 10 } ]
if { $nToJail < 1 || $nToJail != [expr { round( $nToJail ) } ] } { set nToJail 0 }
if { $nToJail != 0 } { ::zJail::JailPlayer $player $nToJail "[::Texts::Get why_killed_cililians $KillCivilianCount]" } } }
::Custom::HookProc "::WoWEmu::CalcXP" { ::zJail::AutoJailbyKillCivilian $killer $victim } }
if { [info procs "::SpellEffects::ScriptLogin"] == "" } {
proc ::zJail::OnLogin { to from spellid } {
::Custom::DbArray set ::Custom::PlayerID [ list [ string tolower [ ::GetName $to ] ] $to ] }
::Custom::AddSpellScript "::zJail::OnLogin" 836 }
if { $::zJail::NoCmdTeleIfJailed } {
if { [info procs "::zBefehle::tele"] != "" } {::Custom::HookProc "::zBefehle::tele" {::zJail::Check $player}}
if { [info procs "::WoWEmu::Commands::go"] != "" } {::Custom::HookProc "::WoWEmu::Commands::go" {::zJail::Check $player}}
if { [info procs "::WoWEmu::Commands::goname"] != "" } {::Custom::HookProc "::WoWEmu::Commands::goname" {::zJail::Check $player}}
if { [info procs "::WoWEmu::Commands::goguid"] != "" } {::Custom::HookProc "::WoWEmu::Commands::goguid" {::zJail::Check $player}}
if { [info procs "::WoWEmu::Commands::gotrigger"] != "" } {::Custom::HookProc "::WoWEmu::Commands::gotrigger" {::zJail::Check $player}}
}

namespace eval ::zJailNPC { }
proc ::zJailNPC::GossipHello { npc player } {
set option0 "text 6 \"[::Texts::Get how_long]\""
set option1 "text 0 \"[::Texts::Get why_jailed]\""
set option2 "text 0 \"[::Texts::Get let_me_free]\""
::SendGossip $player $npc $option0 $option1 $option2
::Emote $npc 66
::Emote $player 66
}
proc ::zJailNPC::GossipSelect { npc player option } {
	set pname [ ::GetName $player ]
	switch $option {
		0 {
			if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } {
				set xfile [open "$::zJail::JAIL_DIR/$pname"]
				set data [read $xfile]
				close $xfile
				if { [ lindex $data 0 ] > [clock seconds] } { set restmsg "(Rest: [::Custom::SecondsToTime [ expr { [ lindex $data 0 ] - [clock seconds] } ]])"
				} else { set restmsg [::Texts::Get npc_not_jailed] }
				if { [ lindex $data 0 ] != 0 } { set jailedtill "[::Texts::Get npc_jailed_till] [clock format [ lindex $data 0 ] -format "%d.%m (%Hh:%Mm)"]. $restmsg"
				} else { set jailedtill [::Texts::Get npc_jailed_gmfree] }
				::Say $npc 0 $jailedtill
			} else { ::Say $npc 0 [::Texts::Get npc_not_jailed2] }
			SendGossipComplete $player
		}
		1 {
			if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } {
				set xfile [open "$::zJail::JAIL_DIR/$pname"]
				set data [read $xfile]
				close $xfile
				if { [ lindex $data 1 ] != 0 } { set reason "[::Texts::Get npc_reason] [lrange $data 1 end]"
				} else { set reason [::Texts::Get npc_no_reason] }
				::Say $npc 0 $reason
			} else { ::Say $npc 0 [::Texts::Get npc_not_jailed2] }
			SendGossipComplete $player
		}
		2 {
			if { [ file exists "$::zJail::JAIL_DIR/$pname" ] } {
				set xfile [open "$::zJail::JAIL_DIR/$pname"]
				set data [read $xfile]
				close $xfile
				if { [ lindex $data 0 ] != 0 } { set jailedtime [ lindex $data 0 ]
				} else {
				::Say $npc 0 [::Texts::Get npc_jailed_gmfree]
				SendGossipComplete $player
				return
				}
				if { [clock seconds] > $jailedtime } {
				::Say $player 0 [::Texts::Get npc_back_home]
				::Custom::GoHome $player
				file delete "$::zJail::JAIL_DIR/$pname"
				SendGossipComplete $player
				return
				} else {
				set waittime [::Custom::SecondsToTime [ expr { [ lindex $data 0 ] - [clock seconds] } ]]
				::Say $npc 0 [::Texts::Get npc_wait_gmfree $waittime]
				SendGossipComplete $player
				return
				}
			} else {
			::Custom::GoHome $player
			::Say $player 0 [::Texts::Get player_now_notjailed]
			SendGossipComplete $player
			return
			}
		}
	}
}
